
/* 搜索框 */
$('.sel_input>input').on('input',function(){
    console.log(this.value)
    const text = this.value
    $.ajax({
        url:'/shop_sel',
        data:{
            ajax: 1,
            csrf_token: 'c319200382fddb6a3387f297dea14840',
            logid: 3439079469256432605,
            fid: '0,1633741091629',
            _: new Date().getTime(),
            q: text,
        },
        dataType:'json',
        success:function(res){
            const arr = res.data[1].data
            let text = ''
            arr.forEach(item=>{
                text += `
                <li>
                    <span>
                        ${item.word}
                    </span>
                    <p>
                        约${item.skuSize}个
                    </p>
                </li>
                `
                
            })
            $(".sel_result").css('display','block')
            $(".sel_result").html(text)
        },
        error:function(err){
            $(".sel_result").html('暂无结果')
        }
    })
    
})
$('.sel_input>input').focus(function(){
    $('.sel_result').css('display','block')
})
$('.sel_input>input').blur(function(){
    $('.sel_result').css('display','none')
})
/* 渲染分类列表 */
$.ajax({
    url:'/list',
    data:{
        ajax: 1,
        csrf_token: 'c319200382fddb6a3387f297dea14840',
        logid: 3439097983399026058,
        fid: '0,1634088160309',
        _: new Date().getTime(),
        q: '触控产品',
        from: 'index_menu',
        tf:'' ,
        pi: 'b2b.index.menu',
        o: 0,
        p: 2,
        mk: '全部结果',
        f: [],
        s: 30,
        adn: 3,
        resType: 'product',
        fn: {"select_param0":"品牌","select_param1":"分辨率","select_param2":"尺寸"},
    },
    success:function(res){
        /* 渲染分类标题 */
        const sort = res.data.filterNames
        const sortLi = res.data.filter
        let text = ''
        ;for(var key in sort){
            text += `
                <div>
                    <p>${sort[key]}:</p>
                    <ul>
                        
                    </ul>
                    <span class="more">更多<i class="iconfont icon-xiajiantou"></i></span>
                    <span class="hide">收起<i class="iconfont icon-xiajiantou"></i></span>
                </div>
            `
        }
        $('.sort_filter').html(text)
        /* 渲染分类内容 */
        let flage = 0
        for(var key in sortLi){
            let textLi = ''
            sortLi[key].forEach(item=>{
                textLi += `<li>${item}</li>`
            })
            $('.sort_filter>div>ul').eq(flage).html(textLi)
            flage++
        }
        /* 更多按钮点击事件 */
        $('.sort_filter>div>.more').click(function(){
            $(this).css('display','none')
            $(this).next().css('display','block')
            $(this).prev().css('height','auto')
        })
        $('.sort_filter>div>.hide').click(function(){
            $(this).css('display','none')
            $(this).prev().css('display','block')
            $(this).prev().prev().css('height',30)
        })
    },
    error:function(err){
        console.log(err)
    }
})
/* 人气排序那一行点击事件 */
$('.synthesize_filter>p').click(function(){
    $(this).addClass('synthesize_filter_active').siblings().removeClass('synthesize_filter_active')
})
var info = {
    ajax: 1,
    csrf_token: 'c319200382fddb6a3387f297dea14840',
    logid: 3439097983399026058,
    fid: '0,1634088160309',
    _: new Date().getTime(),
    q: '触控产品',
    from: 'index_menu',
    tf:'' ,
    pi: 'b2b.index.menu',
    o: 0,
    p: 1,
    mk: '全部结果',
    f: [],
    s: 30,
    adn: 3,
    resType: 'product',
    fn: {"select_param0":"品牌","select_param1":"分辨率","select_param2":"尺寸"},
}
function moreAjax(info){
    $.ajax({
        url:'/list',
        data:info,
        success:function(res){
            console.log(res.data.productList)
            const data = res.data.productList
            let text = ''
            data.forEach((item,index,arr)=>{
                text += `
                <li data-id="${index}">
                    <a href="./particulars.html">
                        <img src="${item.picUrl}" alt="">
                        <span class="price">￥${item.price}${item.pCurrency}</span>
                        <span class="title">${item.fullName}</span>
                        <span class="trait">厂家直销</span>
                        <p>
                            <span>${item.fullProviderName}</span>
                            <span>${item.location}</span>
                        </p>
                    </a>
                </li>
                
                `
            })
            $('.all_result>ul').html(text)

            /* 点击将数据存到localstorage */
            $('.all_result>ul>li').click(function(){
                const foodsId = $(this).attr('data-id')
                localStorage.setItem('foodsId',JSON.stringify(data[foodsId]))
            })
        },
        error:function(err){
            console.log(err)
        }
    })
}
moreAjax(info)
$('.synthesize_filter').bind('click','p',e=>{
    const target = $(e.target)
    if(target.prop('id') === 'default'){
        moreAjax(info)
    }
    if(target.prop('id') === 'hot'){
        let copy = info
        copy.o = 1
        copy.p = 1
        copy.adn = 0
        moreAjax(copy)
        $('.all_result .now').text(copy.p)
    }
    if(target.prop('id') === 'low_height'){
        let copy = info
        copy.o = 2
        copy.p = 1
        copy.adn = 0
        moreAjax(copy)
        $('.all_result .now').text(copy.p)
    }
    if(target.prop('id') === 'height_low'){
        let copy = info
        copy.o = 3
        copy.p = 1
        copy.adn = 0
        moreAjax(copy)
        $('.all_result .now').text(copy.p)
    }
    if(target.prop('id') === 'btn'){
        const low = $('.synthesize_filter>#low').val()
        const height = $('.synthesize_filter>#height').val()
        let copy = info
        copy.o = 0
        copy.p = 1
        copy.f = `[{"price":[${low},${height}]}]`
        copy.adn = 0
        moreAjax(copy)
        $('.all_result .now').text(copy.p)
    }
})
/* 上一页下一页 */
/* const nowPage = $('.all_result .now').text() */
$('.all_result .prev').click(function(){
    if($('.all_result .now').text() == 1){
        $('.all_result .prev').attr('disabled',"true")
    }
    let copy = info
    copy.p = copy.p - 1
    moreAjax(copy)
    $('.all_result .now').text(copy.p)
    $('html,body').animate({scrollTop:0},0)
})
$('.all_result .next').click(function(){
    let copy = info
    copy.p = copy.p + 1
    moreAjax(copy)
    $('.all_result .now').text(copy.p)
    $('html,body').animate({scrollTop:0},0)
})